package com.luomo.java.juc.util;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author LiuMei
 * @date 2021-04-08.
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() ->
                System.out.println(Thread.currentThread().getName() + "\t没有返回值"));
        completableFuture.get();
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\tcompleteFuture2");
//            int age = 10 / 0;
            return 1024;
        });
        System.out.println(completableFuture1.whenComplete((t, u) -> {
            System.out.println("---t:" + t);
            System.out.println("---u:" + u);
        }).exceptionally(e -> {
            System.out.println(e.getMessage());
            return 4444;
        }).get());
    }

}
